package com.atguigu.jxc.controller;/*
 *@author 尹凯瑞
 *@date 2020/12/5 8:52
 */

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("damageListGoods")
public class DamageController {

    @Autowired
    private DamageService damageService;


    //保存报损单 http://localhost:8080/damageListGoods/save?damageNumber=BS1605766644460（报损单号,前端生成）
    @RequestMapping("/save")
    public ServiceVO save(DamageList damageList, String damageListGoodsStr, HttpSession session){
        //获取用户信息
        User user = (User)session.getAttribute("currentUser");
        //获取用户id
        Integer userId = user.getUserId();
        //获取用户trueName
        String trueName = user.getTrueName();
        //设置用户id
        damageList.setUserId(userId);
        damageList.setTrueName(trueName);

        Gson gson = new Gson();
        Type type = new TypeToken<List<DamageListGoods>>(){}.getType();
        //将String转换为List
        List<DamageListGoods> damageListGoodsList = gson.fromJson(damageListGoodsStr, type);
        return damageService.saveDamageList(damageList,damageListGoodsList);
    }
    //报讯单查询 http://localhost:8080/damageListGoods/list
    @RequestMapping("/list")
    public Map<String,Object> list(String sTime, String eTime){
        return damageService.selectDamageList(sTime,eTime);
    }
    //报损单商品查询 http://localhost:8080/damageListGoods/goodsList
    @RequestMapping("/goodsList")
    public Map<String,Object> goodsList(Integer damageListId){
        return damageService.selectDamageGoodsList(damageListId);
    }
}
